package com.mt.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.enums.FilmListType;
import com.mt.enums.Status;
import com.mt.exception.GlobalException;
import com.mt.feign.OrderApiProvider;
import com.mt.mapper.FilmCommentMapper;
import com.mt.pojo.FilmComment;
import com.mt.service.CommentLikeService;
import com.mt.service.FilmCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2020-11-18
 */
@Service
public class FilmCommentServiceImpl extends ServiceImpl<FilmCommentMapper, FilmComment> implements FilmCommentService {

    @Resource
    FilmCommentMapper commentMapper;

    @Resource
    CommentLikeService likeService;

    @Resource
    OrderApiProvider orderApiProvider;

    /**
     * todo 1.增删改(需要对user,order进行判断,状态是否完成)
     * 对电影进行评论
     */
    @Override
    public boolean addFilmComment(FilmComment comment) {
        FilmComment existComment = commentMapper.selectOne(Wrappers.<FilmComment>lambdaQuery().eq(FilmComment::getFilmId, comment.getFilmId())
                .eq(FilmComment::getUserId, comment.getUserId()));
        if (existComment == null) {
            int insert = commentMapper.insert(comment);
            if (comment.getScore() < 0 || comment.getScore() > 10) {
                new GlobalException(Status.PARAM_ERROR);
            }
            return insert > 0;
        } else {
            throw new GlobalException(Status.EXIST_COMMENT);
        }
    }

    @Override
    public boolean checkUserIsComment(String userId, int filmId) {
        FilmComment existComment = commentMapper.selectOne(Wrappers.<FilmComment>lambdaQuery().eq(FilmComment::getFilmId, filmId)
                .eq(FilmComment::getUserId, userId));
        return existComment == null;

    }

    /**
     * 分页查询
     * 得到某部电影的评论
     */
    @Override
    public List<FilmComment> pageCommentByFilmId(int currentPage, int pageSize, String userId,
                                                 int filmId, FilmListType listType) {
        List<FilmComment> result = CollUtil.newArrayList();
        Page<FilmComment> page = new Page<>(currentPage, pageSize);
        result = commentMapper.listCommentByFilmId(page, filmId, userId, FilmListType.TypeCode(listType));
        if (!result.isEmpty()) {
            setCommentLikeStatues(userId, result);
        }
        return result;
    }

    /**
     * 删除评论
     */
    @Override
    public boolean deleteComment(int CommentId) {
        int delete = commentMapper.deleteById(CommentId);
        if (delete <= 0){
            throw  new GlobalException(Status.DELETE_ERROR);
        }
        return true;
    }

    @Override
    public FilmComment getUserComments(String userId, int filmId) {
        FilmComment comment = commentMapper.getUserComments(userId, filmId);
        if (comment == null){
            return new FilmComment();
        }
        int likeStatus = likeService.getCommentLikeStatus(userId, comment.getId());
        comment.setLikeStatus(likeStatus);
        return comment;
    }

    /**
     * 获取评论列表的点赞状态
     */
    private void setCommentLikeStatues(String userId, List<FilmComment> result) {
        Iterator<FilmComment> iterator = result.iterator();
        // todo 逻辑处理待完善
        if ("".equals(userId)) {
            if (iterator.hasNext()) {
                FilmComment comment = iterator.next();
                comment.setLikeStatus(0);
            }
        } else {
            if (iterator.hasNext()) {
                FilmComment comment = iterator.next();
                int likeStatus = likeService.getCommentLikeStatus(userId, comment.getId());
                comment.setLikeStatus(likeStatus);
            }
        }
    }

}
